package com.xxdh.controller.course;

import com.xxdh.domain.dto.CourseDto;
import com.xxdh.domain.po.Course;
import com.xxdh.domain.vo.CourseVo;
import com.xxdh.service.ICourseService;
import com.xxdh.util.Result;
import com.xxdh.util.UserHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * 课程相关接口
 */
@RestControllerAdvice
@RequestMapping("/course")
@RequiredArgsConstructor
@Slf4j
@CrossOrigin(origins = "*")
public class CourseController {
    private final ICourseService courseService;
    /**
     * 添加课程
     * @param courseDto
     * @return
     */
    @PostMapping("/add")
    public String addCourse(@RequestBody CourseDto courseDto) {
        courseService.addCourse(courseDto);
        return Result.ok().toJSONString();
    }

    /**
     * 删除课程
     */
    @PostMapping("/delete")
    public String deleteCourseByCourseId(@RequestParam("courseId") String courseId) {
        courseService.deleteCourseByCourseId(courseId);
        return Result.ok().toJSONString();
    }

    /**
     * 老师获取课程列表
     * @return
     */
    @GetMapping("/get-course-list/teacher")
    public String getCourseListByTeacherId() {
        Long teacherId = UserHolder.getUserId();
        Map<String, List<CourseVo>> courses = courseService.getCourseListMapByTeacherId(teacherId);
        return Result.ok(courses).toJSONString();
    }

    /**
     * 学生获取课程列表
     * @return
     */
    @GetMapping("/get-course-list/student")
    public String getCourseListByUserId() {
        Map<String, List<CourseVo>> courseInfos = courseService.getCourseListByUserId();
        return Result.ok(courseInfos).toJSONString();
    }

    /**
     * 学生选课
     * @param courseId 课程id
     * @return
     */
    @PostMapping("/select-course")
    public String selectCourse(@RequestParam("courseId") String courseId) {
        courseService.selectCourse(courseId);
        return Result.ok().toJSONString();
    }

    @GetMapping("/get-course-list/not-selected")
    public String getNotSelectedCourseList() {
        Long userId = UserHolder.getUserId();
        List<CourseVo> courses = courseService.getNotSelectedCourseListByStudentId(userId);
        return Result.ok(courses).toJSONString();
    }
}
